package com.itboye.mengwu.app.adapter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.alert.LoadingDialog;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.ParseDouble;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.MainApplication;
import com.itboye.mengwu.app.ui.main.PingLunBaoBeiActivity;
import com.itboye.mengwu.app.ui.main.TuiKuanXiangQingActivity;
import com.itboye.mengwu.app.ui.mymeng.ActivityShenQingShouHou;
import com.itboye.mengwu.app.ui.web.WebActivity;
import com.itboye.mengwu.buy.ui.PayTypeActivity;
import com.itboye.mengwu.buy.ui.QueRenDingDan;
import com.itboye.mengwu.data.model.OrderBean;
import com.itboye.mengwu.data.model.OrderDetailBean;
import com.itboye.mengwu.data.model.OrderQueryModel;
import com.itboye.mengwu.data.model.OrderDetailBean.ItemsInfo;
import com.itboye.mengwu.data.model.OrderQueryModel.OrderList;
import com.itboye.mengwu.data.model.ShopCarBeanCopy.StoreList;
import com.itboye.mengwu.data.presenter.OrderPresenter;
import com.itboye.mengwu.widget.CustomDialog;

/***
 * 待收货：查看物流、申请售后、确认收货 待付款：取消订单、去支付 待评价：去评价、查看物流 待发货：申请售后 交易关闭：无按钮
 * 
 * @author dsawe
 * 
 */
public class AllOrderQueryAdapter extends BaseAdapter implements Observer {
	OrderPresenter orderPresenter;
	private ArrayList<OrderQueryModel.OrderList> orderQueryModel;
	Activity context;
	protected CustomDialog dialog;
	private String orderCodeTemp;
	private String uid;
	private AlertDialog alert;

	public AllOrderQueryAdapter(
			ArrayList<OrderQueryModel.OrderList> orderQueryModel,
			Activity context) {
		this.orderQueryModel = orderQueryModel;
		this.context = context;
		orderPresenter = new OrderPresenter(this);
		progressDialog = new LoadingDialog();
		uid = SPUtils.get(context, null, SpPublicParams.UID, "") + "";
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return orderQueryModel == null ? 0 : orderQueryModel.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return orderQueryModel == null ? 0 : orderQueryModel;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		int count = 0;
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = CreateView();
			viewHolder.cancle_pay = (Button) convertView
					.findViewById(R.id.cancle_pay);
			viewHolder.go_pay = (Button) convertView.findViewById(R.id.go_pay);
			viewHolder.total_num_tv = (TextView) convertView
					.findViewById(R.id.total_num_tv);
			viewHolder.total_price = (TextView) convertView
					.findViewById(R.id.total_price);
			viewHolder.wait_pay_iv = (ImageView) convertView
					.findViewById(R.id.wait_pay_iv);
			viewHolder.txt_yunfei = (TextView) convertView
					.findViewById(R.id.yunfei);
			viewHolder.txt_oldPrice = (TextView) convertView
					.findViewById(R.id.goods_old_price);
			viewHolder.goodsgroup = (LinearLayout) convertView
					.findViewById(R.id.goodsgroup);
			viewHolder.wait_pay_tv = (TextView) convertView
					.findViewById(R.id.wait_pay_tv);
			viewHolder.other = (Button) convertView.findViewById(R.id.other);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (orderQueryModel.get(position).getPayStatus().equals("0")) {
			if (orderQueryModel.get(position).getOrderStatus().equals("8")) {
				viewHolder.wait_pay_tv.setText("取消或交易关闭");
				viewHolder.cancle_pay.setVisibility(View.GONE);
				viewHolder.go_pay.setVisibility(View.GONE);
				viewHolder.other.setVisibility(View.GONE);
			} else {
				viewHolder.wait_pay_tv.setText("待付款");
				viewHolder.cancle_pay.setVisibility(View.GONE);
				viewHolder.go_pay.setVisibility(View.VISIBLE);
				viewHolder.go_pay.setText("去支付");
				viewHolder.go_pay.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						goPay(v);
					}
				});
				viewHolder.other.setVisibility(View.VISIBLE);
				viewHolder.other.setText("取消订单");

				viewHolder.other.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						cancelOrder(v);
					}
				});
			}
		} else if (orderQueryModel.get(position).getPayStatus().equals("1")) {
			viewHolder.wait_pay_tv.setText("已付款");
			if (orderQueryModel.get(position).getOrderStatus().equals("2")) {
				viewHolder.cancle_pay.setVisibility(View.GONE);
				viewHolder.go_pay.setVisibility(View.GONE);
				viewHolder.other.setVisibility(View.VISIBLE);
				viewHolder.other.setText("申请售后");
				viewHolder.other.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						shenqingShouhou(v);
					}
				});
				viewHolder.wait_pay_tv.setText("待发货");
			} else if (orderQueryModel.get(position).getOrderStatus()
					.equals("3")) {
				viewHolder.cancle_pay.setVisibility(View.GONE);
				viewHolder.go_pay.setVisibility(View.GONE);
				viewHolder.other.setVisibility(View.VISIBLE);
				viewHolder.other.setText("申请售后");
				viewHolder.other.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						shenqingShouhou(v);
					}
				});
				viewHolder.wait_pay_tv.setText("待发货");
			} else if (orderQueryModel.get(position).getOrderStatus()
					.equals("4")) {
				viewHolder.cancle_pay.setVisibility(View.VISIBLE);
				viewHolder.cancle_pay.setText("查看物流");
				viewHolder.cancle_pay.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						QueryWuliu(v);
					}
				});
				viewHolder.go_pay.setVisibility(View.VISIBLE);
				viewHolder.go_pay.setText("申请售后");
				viewHolder.go_pay.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						shenqingShouhou(v);
					}
				});
				viewHolder.other.setVisibility(View.VISIBLE);
				viewHolder.other.setText("确认收货");
				viewHolder.other.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						makesureGetGoods(v);
					}
				});
				viewHolder.wait_pay_tv.setText("待收货");
			} else if (orderQueryModel.get(position).getOrderStatus()
					.equals("5")) {
				viewHolder.wait_pay_tv.setText("待评价");
				if (orderQueryModel.get(position).getCommentStatus() != null) {
					if (orderQueryModel.get(position).getCommentStatus()
							.equals("0")) {
						viewHolder.wait_pay_tv.setText("待评论");
						viewHolder.cancle_pay.setVisibility(View.GONE);
						viewHolder.go_pay.setVisibility(View.VISIBLE);
						viewHolder.go_pay.setText("去评价");
						viewHolder.go_pay
								.setOnClickListener(new OnClickListener() {

									@Override
									public void onClick(View v) {
										// TODO Auto-generated method stub
										GotoComment(v);
									}
								});
						viewHolder.other.setVisibility(View.VISIBLE);
						viewHolder.other.setText("查看物流");
						viewHolder.other
								.setOnClickListener(new OnClickListener() {

									@Override
									public void onClick(View v) {
										// TODO Auto-generated method stub
										QueryWuliu(v);
									}
								});
					} else if (orderQueryModel.get(position).getCommentStatus()
							.equals("1")) {
						viewHolder.wait_pay_tv.setText("已评论");
						viewHolder.cancle_pay.setVisibility(View.GONE);
						viewHolder.go_pay.setVisibility(View.GONE);
						viewHolder.other.setVisibility(View.VISIBLE);
						viewHolder.other.setText("查看物流");
						viewHolder.other
								.setOnClickListener(new OnClickListener() {

									@Override
									public void onClick(View v) {
										// TODO Auto-generated method stub
										QueryWuliu(v);
									}
								});
					} else if (orderQueryModel.get(position).getCommentStatus()
							.equals("2")) {
						viewHolder.wait_pay_tv.setText("超时,系统自动评论");
						viewHolder.cancle_pay.setVisibility(View.GONE);
						viewHolder.go_pay.setVisibility(View.GONE);
						viewHolder.other.setVisibility(View.VISIBLE);
						viewHolder.other.setText("查看物流");
						viewHolder.other
								.setOnClickListener(new OnClickListener() {

									@Override
									public void onClick(View v) {
										// TODO Auto-generated method stub
										QueryWuliu(v);
									}
								});
					}
				}
			} else if (orderQueryModel.get(position).getOrderStatus()
					.equals("6")) {
				viewHolder.wait_pay_tv.setText("已退货");
			} else if (orderQueryModel.get(position).getOrderStatus()
					.equals("7")) {
				viewHolder.wait_pay_tv.setText("已完成");
			} else if (orderQueryModel.get(position).getOrderStatus()
					.equals("8")) {
				viewHolder.wait_pay_tv.setText("取消或交易关闭");
			} else if (orderQueryModel.get(position).getOrderStatus()
					.equals("9")) {
				viewHolder.wait_pay_tv.setText("已付款正在退款");
			}
			if (orderQueryModel.get(position).getCsStatus().equals("2")) {
				viewHolder.wait_pay_tv.setText("售后待处理");
			} else if (orderQueryModel.get(position).getCsStatus().equals("3")) {
				viewHolder.wait_pay_tv.setText("售后处理完成");
			}

		} else if (orderQueryModel.get(position).getPayStatus().equals("2")) {
			viewHolder.wait_pay_tv.setText("已退款");
		} else if (orderQueryModel.get(position).getPayStatus().equals("3")) {
			viewHolder.wait_pay_tv.setText("支付中");
		} else if (orderQueryModel.get(position).getPayStatus().equals("5")) {
			viewHolder.wait_pay_tv.setText("货到付款");
		}

		viewHolder.other.setTag(position);
		viewHolder.go_pay.setTag(position);
		viewHolder.other.setTag(position);
		// if (orderQueryModel.get(position).getPayStatus().equals("0")) {
		// if (orderQueryModel.get(position).getOrderStatus().equals("8")) {
		// viewHolder.wait_pay_tv.setText("交易已关闭");
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.GONE);
		// viewHolder.other.setVisibility(View.GONE);
		// } else {
		// viewHolder.wait_pay_tv.setText("待付款");
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.VISIBLE);
		// viewHolder.other.setText("取消订单");
		//
		// viewHolder.other.setOnClickListener(new OnClickListener() {// 取消订单
		//
		// @Override
		// public void onClick(final View v) {
		// // TODO Auto-generated method stub
		// CustomDialog.Builder customBuilder = new CustomDialog.Builder(
		// context);
		// customBuilder
		// .setTitle("取消订单")
		// .setMessage("是否取消该订单")
		// .setNegativeButton(
		// "取消",
		// new DialogInterface.OnClickListener() {
		// public void onClick(
		// DialogInterface dialog,
		// int which) {
		// dialog.dismiss();
		// }
		// })
		// .setPositiveButton(
		// "确定",
		// new DialogInterface.OnClickListener() {
		//
		// public void onClick(
		// DialogInterface dialog,
		// int which) {
		// showProgressDialog("订单处理中,请稍后",true);
		// OrderBean model = new OrderBean();
		// model.setUid(SPUtils
		// .get(MainApplication.context,
		// null,
		// SpPublicParams.UID,
		// "")
		// + "");
		// orderCodeTemp = orderQueryModel
		// .get((int) v
		// .getTag())
		// .getOrderCode()
		// + "";
		// model.setCode(orderQueryModel
		// .get((int) v
		// .getTag())
		// .getOrderCode()
		// + "");
		// orderPresenter
		// .delete(model);
		//
		// dialog.dismiss();
		// }
		// });
		// dialog = customBuilder.create();
		// dialog.show();
		// }
		// });
		// viewHolder.go_pay.setText("去支付");
		// /**
		// * Intent intent = new Intent(getActivity(),
		// * PayTypeActivity.class); OrderBean bean = new OrderBean();
		// * bean.setTotalPrice(Double.parseDouble(model.get(position).
		// * getPrice
		// * ())-Double.parseDouble(model.get(position).getDiscountMoney
		// * ())
		// * -Double.parseDouble(model.get(position).getPayBalance())+"");
		// * bean.setCode(model.get(position).getOrderCode());
		// * bean.setDesc(model.get(position).getOrderCode());
		// * intent.putExtra("model", bean); startActivity(intent);
		// */
		// viewHolder.go_pay.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(context,
		// PayTypeActivity.class);
		// OrderBean bean = new OrderBean();
		// bean.setTotalPrice(Double.parseDouble(orderQueryModel
		// .get((int) v.getTag(R.id.tag_first)).getPrice())
		// - Double.parseDouble(orderQueryModel.get(
		// (int) v.getTag(R.id.tag_first))
		// .getDiscountMoney())
		// - Double.parseDouble(orderQueryModel.get(
		// (int) v.getTag(R.id.tag_first))
		// .getPayBalance()) + "");
		// bean.setCode(orderQueryModel.get(
		// (int) v.getTag(R.id.tag_first)).getOrderCode());
		// bean.setDesc(orderQueryModel.get(
		// (int) v.getTag(R.id.tag_first)).getOrderCode());
		// intent.putExtra("model", bean);
		// context.startActivity(intent);
		// }
		// });
		// viewHolder.cancle_pay.setOnClickListener(new OnClickListener() {//
		// 去支付
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(
		// MainApplication.context,
		// PayTypeActivity.class);
		// OrderBean bean = new OrderBean();
		// bean.setTotalPrice(Double
		// .parseDouble(orderQueryModel.get(
		// (int) v.getTag()).getPrice())
		// - Double.parseDouble(orderQueryModel
		// .get((int) v.getTag())
		// .getDiscountMoney())
		// - Double.parseDouble(orderQueryModel
		// .get((int) v.getTag())
		// .getPayBalance()) + "");
		// bean.setCode(orderQueryModel.get(
		// (int) v.getTag()).getOrderCode());
		// bean.setDesc(orderQueryModel.get(
		// (int) v.getTag()).getOrderCode());
		// intent.putExtra("model", bean);
		// context.startActivity(intent);
		// }
		// });
		// viewHolder.other.setVisibility(View.VISIBLE);
		// }
		// // viewHolder.cancle_pay.setVisibility(View.GONE);
		// } else {
		// if (orderQueryModel.get(position).getOrderStatus().equals("3")
		// || orderQueryModel.get(position).getOrderStatus()
		// .equals("2")) {
		// if (orderQueryModel.get(position).getCsStatus().equals("2"))
		// viewHolder.wait_pay_tv.setText("售后处理中");
		// else if (orderQueryModel.get(position).getCsStatus()
		// .equals("3"))
		// viewHolder.wait_pay_tv.setText("售后已处理");
		// else {
		// viewHolder.wait_pay_tv.setText("待发货");
		// }
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.GONE);
		// viewHolder.other.setVisibility(View.VISIBLE);
		// viewHolder.other.setText("申请售后");
		// viewHolder.other.setOnClickListener(new OnClickListener() {// 申请售后
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(
		// MainApplication.context,
		// ActivityShenQingShouHou.class);
		// OrderDetailBean bean = new OrderDetailBean();
		// bean.setOrderCode(orderQueryModel.get(
		// (int) v.getTag()).getOrderCode());
		// List<OrderDetailBean.ItemsInfo> info = new
		// ArrayList<OrderDetailBean.ItemsInfo>();
		// for (int j = 0; j < orderQueryModel
		// .get((int) v.getTag()).getItems()
		// .size(); j++) {
		// ItemsInfo info2 = new ItemsInfo();
		// info2.setCount(orderQueryModel
		// .get((int) v.getTag()).getItems()
		// .get(j).getCount());
		// info2.setName(orderQueryModel
		// .get((int) v.getTag()).getItems()
		// .get(j).getName());
		// info2.setPrice(orderQueryModel
		// .get((int) v.getTag()).getItems()
		// .get(j).getPrice());
		// info2.setImg(orderQueryModel
		// .get((int) v.getTag()).getItems()
		// .get(j).getImgUrl());
		// info.add(info2);
		// }
		// bean.setItems(info);
		// intent.putExtra("model", bean);
		// context.startActivity(intent);
		// }
		// });
		// } else if (orderQueryModel.get(position).getOrderStatus()
		// .equals("4")) {
		// viewHolder.wait_pay_tv.setText("待收货");
		// } else if (orderQueryModel.get(position).getOrderStatus()
		// .equals("5")) {
		// viewHolder.wait_pay_tv.setText("待评价");
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.GONE);
		// viewHolder.other.setText("查看详情");
		// viewHolder.other.setOnClickListener(new OnClickListener() {// 查看详情
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(
		// MainApplication.context,
		// TuiKuanXiangQingActivity.class);
		// intent.putExtra("orderId",
		// orderQueryModel.get((int) v.getTag())
		// .getId());
		// context.startActivity(intent);
		// }
		// });
		// } else if (orderQueryModel.get(position).getOrderStatus()
		// .equals("8")) {
		// viewHolder.wait_pay_tv.setText("交易关闭");
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.GONE);
		// viewHolder.other.setVisibility(View.GONE);
		// } else if (orderQueryModel.get(position).getOrderStatus()
		// .equals("9")) {
		// viewHolder.wait_pay_tv.setText("已付款正在退款");
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.GONE);
		// viewHolder.other.setVisibility(View.GONE);
		// }
		// if (orderQueryModel.get(position).getCsStatus().equals("2")){
		// viewHolder.wait_pay_tv.setText("售后处理中");
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.GONE);
		// viewHolder.other.setVisibility(View.GONE);
		// }else if (orderQueryModel.get(position).getCsStatus().equals("3")){
		// viewHolder.wait_pay_tv.setText("售后已处理");
		// viewHolder.cancle_pay.setVisibility(View.GONE);
		// viewHolder.go_pay.setVisibility(View.GONE);
		// viewHolder.other.setVisibility(View.GONE);
		// }
		//
		// }
		// viewHolder.cancle_pay.setText("取消订单");
		// viewHolder.cancle_pay.setTag(orderQueryModel.get(position)
		// .getOrderCode());
		viewHolder.cancle_pay.setTag(position);
		// viewHolder.go_pay.setTag(orderQueryModel.get(position).getId());
		viewHolder.go_pay.setTag(R.id.tag_first, position);
		viewHolder.goodsgroup.removeAllViews();
		for (int i = 0; i < orderQueryModel.get(position).getItems().size(); i++) {

			View childView = LayoutInflater.from(context).inflate(
					R.layout.goods_item, null);
			((TextView) childView.findViewById(R.id.dangge_shuliang))
					.setText("x"
							+ orderQueryModel.get(position).getItems().get(i)
									.getCount());
			((TextView) childView.findViewById(R.id.goods_old_price))
					.setText("￥"
							+ orderQueryModel.get(position).getItems().get(i)
									.getOriPrice());
			((TextView) childView.findViewById(R.id.goods_old_price))
					.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); // 中划线
			XImageLoader.load(GlobalConfig.API_URL
					+ "/picture/index?id="
					+ orderQueryModel.get(position).getItems().get(i)
							.getImgUrl(),
					((ImageView) childView.findViewById(R.id.wait_pay_iv)));
			((TextView) childView.findViewById(R.id.goods_price)).setText("￥"
					+ orderQueryModel.get(position).getItems().get(i)
							.getPrice());
			((TextView) childView.findViewById(R.id.goodsname_tv))
					.setText(orderQueryModel.get(position).getItems().get(i)
							.getName());
			((TextView) childView.findViewById(R.id.buy_num))
					.setText(orderQueryModel.get(position).getItems().get(i)
							.getSkuDesc());
			count += Integer.parseInt(orderQueryModel.get(position).getItems()
					.get(i).getCount());
			viewHolder.goodsgroup.addView(childView);
		}

		viewHolder.total_num_tv.setText("共" + count + "件商品");

		viewHolder.total_price.setText("￥"
				+ orderQueryModel.get(position).getPrice() + "");

		viewHolder.txt_yunfei.setText("(含运费￥"
				+ orderQueryModel.get(position).getPostPrice() + ")  优惠 ￥"
				+ orderQueryModel.get(position).getDiscountMoney());
		return convertView;
	}

	/***
	 * 确认收货
	 * 
	 * @param v
	 */
	protected void makesureGetGoods(View v) {
		// TODO Auto-generated method stub
		orderPresenter.makesureOrder(uid, orderQueryModel.get((int) v.getTag())
				.getOrderCode());
		orderCodeTemp = orderQueryModel.get((int) v.getTag()).getOrderCode();
	}

	/***
	 * 去评价
	 * 
	 * @param v
	 */
	protected void GotoComment(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(MainApplication.context,
				PingLunBaoBeiActivity.class);
		intent.putExtra("model", orderQueryModel.get((int) v.getTag()));
		context.startActivity(intent);
	}

	/***
	 * 查看物流
	 * 
	 * @param v
	 */
	protected void QueryWuliu(View v) {
		// TODO Auto-generated method stub

		// ByAlert.alert("7");
		Intent intent = new Intent(MainApplication.context, WebActivity.class);
		intent.putExtra("title", "物流查询");
		intent.putExtra(
				"url",
				SpPublicParams.Logistics_URL
						+ orderQueryModel.get((int) v.getTag()).getOrderCode()
						+ "&uid="
						+ uid
						+ "&psw="
						+ SPUtils.get(MainApplication.context, null,
								SpPublicParams.PASSWORD, ""));
		context.startActivity(intent);

	}

	private View CreateView() {
		View v = null;
		LayoutInflater inflater = LayoutInflater.from(context);
		v = inflater.inflate(R.layout.item_allorder, null);
		return v;
	}

	class ViewHolder {
		private Button cancle_pay, go_pay, other;
		TextView total_num_tv, txt_oldPrice, txt_yunfei, total_price,
				wait_pay_tv;
		ImageView wait_pay_iv;
		LinearLayout goodsgroup;
	}

	/***
	 * 取消订单
	 */

	public void cancelOrder(final View v) {
		// CustomDialog.Builder customBuilder = new CustomDialog.Builder(
		// context);
		// customBuilder
		// .setTitle("取消订单")
		// .setMessage("是否取消该订单")
		// .setNegativeButton(
		// "取消",
		// new DialogInterface.OnClickListener() {
		// public void onClick(
		// DialogInterface dialog,
		// int which) {
		// dialog.dismiss();
		// }
		// })
		// .setPositiveButton(
		// "确定",
		// new DialogInterface.OnClickListener() {
		//
		// public void onClick(
		// DialogInterface dialog,
		// int which) {
		// showProgressDialog("订单处理中,请稍后",true);
		// OrderBean model = new OrderBean();
		// model.setUid(SPUtils
		// .get(MainApplication.context,
		// null,
		// SpPublicParams.UID,
		// "")
		// + "");
		// orderCodeTemp = orderQueryModel
		// .get((int) v
		// .getTag())
		// .getOrderCode()
		// + "";
		// model.setCode(orderQueryModel
		// .get((int) v
		// .getTag())
		// .getOrderCode()
		// + "");
		// orderPresenter
		// .delete(model);
		//
		// dialog.dismiss();
		// }
		// });
		// dialog = customBuilder.create();
		// dialog.show();

		alert = new AlertDialog.Builder(context).create();
		alert.setTitle("操作提示");
		alert.setMessage("是否取消该订单");
		alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// return;
					}
				});
		alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						showProgressDialog("订单处理中,请稍后", true);
						OrderBean model = new OrderBean();
						model.setUid(SPUtils.get(MainApplication.context, null,
								SpPublicParams.UID, "") + "");
						orderCodeTemp = orderQueryModel.get((int) v.getTag())
								.getOrderCode() + "";
						model.setCode(orderQueryModel.get((int) v.getTag())
								.getOrderCode() + "");
						orderPresenter.delete(model);

						dialog.dismiss();
						// return;
					}
				});
		alert.show();
	}

	/***
	 * 去支付
	 * 
	 * @param v
	 */
	public void goPay(final View v) {
		Intent intent = new Intent(MainApplication.context,
				PayTypeActivity.class);
		OrderBean bean = new OrderBean();
		bean.setTotalPrice(Double.parseDouble(orderQueryModel.get(
				(int) v.getTag()).getPrice())
				- Double.parseDouble(orderQueryModel.get((int) v.getTag())
						.getDiscountMoney())
				- Double.parseDouble(orderQueryModel.get((int) v.getTag())
						.getPayBalance()) + "");
		bean.setCode(orderQueryModel.get((int) v.getTag()).getOrderCode()+";");
		System.out.println(orderQueryModel.get((int) v.getTag()).getOrderCode()+">>>?");
		bean.setDesc(orderQueryModel.get((int) v.getTag()).getOrderCode());
		intent.putExtra("model", bean);
		context.startActivity(intent);
	}

	/***
	 * 申请售后
	 * 
	 * @param v
	 */
	public void shenqingShouhou(View v) {
		Intent intent = new Intent(MainApplication.context,
				ActivityShenQingShouHou.class);
		OrderDetailBean bean = new OrderDetailBean();
		bean.setOrderCode(orderQueryModel.get((int) v.getTag()).getOrderCode());
		List<OrderDetailBean.ItemsInfo> info = new ArrayList<OrderDetailBean.ItemsInfo>();
		for (int j = 0; j < orderQueryModel.get((int) v.getTag()).getItems()
				.size(); j++) {
			ItemsInfo info2 = new ItemsInfo();
			info2.setCount(orderQueryModel.get((int) v.getTag()).getItems()
					.get(j).getCount());
			info2.setName(orderQueryModel.get((int) v.getTag()).getItems()
					.get(j).getName());
			info2.setPrice(orderQueryModel.get((int) v.getTag()).getItems()
					.get(j).getPrice());
			info2.setImg(orderQueryModel.get((int) v.getTag()).getItems()
					.get(j).getImgUrl());
			info.add(info2);
		}
		bean.setItems(info);
		intent.putExtra("model", bean);
		context.startActivity(intent);
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == OrderPresenter.OrderDeleteSuccess) {
				ByAlert.alert(entity.getData() + "");
				Iterator<OrderList> itr = orderQueryModel.iterator();
				while (itr.hasNext()) {
					OrderList genTieItemBean = itr.next();
					if (genTieItemBean.getOrderCode().equals(orderCodeTemp)) {
						itr.remove();
					}
				}
				notifyDataSetChanged();
			} else if (entity.getEventType() == OrderPresenter.OrderSureSuccess) {
				ByAlert.alert(entity.getData() + "");
				Iterator<OrderList> itr = orderQueryModel.iterator();
				while (itr.hasNext()) {
					OrderList genTieItemBean = itr.next();
					if (genTieItemBean.getOrderCode().equals(orderCodeTemp)) {
						genTieItemBean.setOrderStatus("5");
						break;
						// itr.remove();
					}
				}
				notifyDataSetChanged();

			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public ResultEntity handlerError(Object data) {
		ResultEntity result = (ResultEntity) data;
		if (result == null) {
			result = new ResultEntity("-1", "数据格式错误!~", data);
			return result;
		}

		if (result.hasError()) {
			ByAlert.alert(result.getMsg());
			result.setEventType("unknow");
			return result;
		}

		return result;
	}

	LoadingDialog progressDialog = null;

	@SuppressLint("NewApi")
	public void showProgressDialog(String message, boolean flag) {
		if (progressDialog == null)
			return;
		setProgressDialogMessage(message);

		progressDialog.show(context.getFragmentManager(), message);
		progressDialog.setCancelable(flag);

	}

	/**
	 * 设置进度对话框消息
	 * 
	 * @param message
	 */
	public void setProgressDialogMessage(String message) {
		progressDialog.setMsg(message);
	}

	/**
	 * 关闭进度对话框
	 */
	@SuppressLint("NewApi")
	public void closeProgressDialog() {
		if (progressDialog != null) {
			if (progressDialog.getDialog().isShowing()) {
				progressDialog.dismiss();
			}
		}
	}

}
